package com.lj.tools.util;

import com.lj.tools.consts.Constants;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateTimeUtil {

    /**
     * yyyy-MM-dd
     * */
    public static SimpleDateFormat getNormalDayFormat() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        format.setTimeZone(Constants.GMT_TIMEZONE);
        return format;
    }

    /**
     * yyyy-MM-dd HH:mm:ss
     * */
    public static SimpleDateFormat getNormalFormat() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        format.setTimeZone(Constants.GMT_TIMEZONE);
        return format;
    }

    /**
     * yyyyMMdd
     * */
    public static SimpleDateFormat getShortDateFormat() {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        format.setTimeZone(Constants.GMT_TIMEZONE);
        return format;
    }

    /**
     * yyyyMMdd'T'HHmmss'Z'
     * */
    public static SimpleDateFormat getLongDateFormat() {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd'T'HHmmss'Z'");
        format.setTimeZone(Constants.GMT_TIMEZONE);
        return format;
    }

    /**
     * yyyy-MM-dd'T'HH:mm:ss.SSS'Z'
     * */
    public static SimpleDateFormat getExpirationDateFormat() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        format.setTimeZone(Constants.GMT_TIMEZONE);
        return format;
    }


    /**
     * 友好文字显示
     * 几天前
     * 几小时前
     * 几分钟前
     * */
    public static String friendly(Date date){
        Calendar calendar = Calendar.getInstance();
        long timeLeft = calendar.getTime().getTime() - date.getTime();
        StringBuilder builder = new StringBuilder();

        if ( timeLeft > 86400000){//天
             builder.append(String.format("%d天前",timeLeft/86400000));
        }else if ( timeLeft > 3600000){//小时
            builder.append(String.format("%d小时前",timeLeft/3600000));
        }else if(timeLeft > 60000){ // 分钟
            builder.append(String.format("%d分钟前",timeLeft/60000));
        }
        return builder.toString();
    }
}
